package com.easylinkin.linkappapi.reportCenter.entity.base;

import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;

import java.io.Serializable;

/**
 * @Description 报告基类
 * @Author kan yuanfeng
 * @Date 2023/7/17 11:39
 */
@Data
public class ReportBase<T extends ReportBase> extends Model<T> {
    @Override
    protected Serializable pkVal() {
        return null;
    }

    /**
     * 上月告警次数
     */
    @TableField(exist = false)
    private Integer lastNum;

    /**
     * 环比
     */
    @TableField(exist = false)
    private String ratio;

    /**
     * 设备名称
     */
    @TableField(exist = false)
    private String deviceName;

    /**
     * 环比
     * @return
     */
    public void computeRatio(){
        Integer curNum = (Integer)ReflectUtil.getFieldValue(this,"num");
        if (null != curNum && null != lastNum){
            if (lastNum == 0) {
                if (curNum==0){
                    this.ratio = "0%";
                }else {
                    this.ratio = String.format("%.2f", ((curNum.doubleValue()) / 1d * 100))+"%";
                }
            } else {
                this.ratio = String.format("%.2f", ((curNum.doubleValue() - lastNum.doubleValue()) / lastNum.doubleValue() * 100))+"%";
            }
        }
    }
}
